/*
 * @Author: dengyl
 * @Date: 2022-01-19 15:10:24
 * @LastEditTime: 2022-01-19 16:51:12
 * @LastEditors: Please set LastEditors
 * @Description: 全局事件
 * @FilePath: \ctp-static-portal-front\src\utils\global.js
 */

import emitter from "./emitter";

/**
 * @description: 监听全局resize事件，并通知变化
 */
window.onresize = debounce(resizeCallback);

// /**
//  * @description: 防抖
//  */
function debounce(fn, delay=1000) {
    var timer = null;

    return function() {
        // 清除上一次延时器
        timer && clearTimeout(timer);
        timer = setTimeout(() => fn.apply(this), delay);
    };
}

// /**
//  * @description: resize回调
//  */
function resizeCallback() {
    const windowW = document.documentElement.clientWidth;
    const windowH = document.documentElement.clientHeight;
    // 下发通知
    emitter.publish('resize', {
        width: windowW,
        height: windowH
    });
}

export default emitter;
